import { defineConfig, type PluginOption } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { internalIpV4 } from "internal-ip";
import { less } from 'svelte-preprocess-less';
import { visualizer } from "rollup-plugin-visualizer";
import { join } from "path";
import monacoEditorPlugin, { IMonacoEditorOpts } from 'vite-plugin-monaco-editor';

const mobile = !!/android|ios/.exec(process.env.TAURI_ENV_PLATFORM);
const monacoEditorPluginDefault = ((monacoEditorPlugin as any).default) as (options: IMonacoEditorOpts) => any;

export default defineConfig(async () => ({
  base: "./",
  build: {
    target: 'esnext'
  },
  plugins: [
    visualizer({
      template: 'treemap'
    }) as PluginOption,
    svelte({
      preprocess: {
        style: less(),
      },
    }),
    monacoEditorPluginDefault({
      languageWorkers: [ 'json' ]
    })
  ],

  clearScreen: false,
  server : {
    port      : 1420,
    strictPort: true,
    host      : mobile ? "0.0.0.0" : false,
    hmr       : mobile
      ? {
        protocol: "ws",
        host    : await internalIpV4(),
        port    : 1421,
      }
      : undefined,
  },
  resolve: {
    alias: {
      '@': join(__dirname, "src/"),
    }
  }
}));
